<script setup>
import { ref, onMounted } from 'vue'
import {
  Menu as IconMenu,
  Setting, User,
} from '@element-plus/icons-vue'
import router from "../router/index.js"
import request from '../utils/request.js'

// 登录状态
const isAuthenticated = ref(false)
const showLoginDialog = ref(false)

// 登录表单数据
const loginForm = ref({
  username: '',
  password: ''
})

// 页面跳转
function handleClick(path) {
  router.push(path)
}

// 登录验证
const login = async () => {
  try {
    const response = await request.post('/api/check/isAdmin',{}, {
      params: {
        username: loginForm.value.username,
        password: loginForm.value.password
        }
    })
    console.log(response)
    
    if (response.data.code === 200) {
      isAuthenticated.value = true
      showLoginDialog.value = false
      ElMessage.success('登录成功')
    } else {
      ElMessage.error(response.data.message || '用户名或密码错误')
    }
  } catch (error) {
    ElMessage.error('登录失败，请检查网络连接')
  }
}

// 退出登录
const logout = () => {
  isAuthenticated.value = false
  showLoginDialog.value = true
  loginForm.value.username = ''
  loginForm.value.password = ''
}

// 页面加载时检查登录状态
onMounted(() => {
  showLoginDialog.value = true
})
</script>

<template>
  <div class="common-layout">
    <!-- 登录对话框 -->
    <el-dialog 
      v-model="showLoginDialog" 
      title="管理员登录" 
      width="400px" 
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false">
      <el-form :model="loginForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            @keyup.enter="login" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
            @keyup.enter="login" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClick('/')">取消</el-button>
          <el-button type="primary" @click="login">登录</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 管理系统界面 -->
    <el-container v-if="isAuthenticated">
      <el-header class="header">
        <div class="header-content">
          <span class="title">后台管理系统</span>
          <el-button 
            type="primary" 
            @click="handleClick('/')" 
            class="back-button"
            size="small">
            返回展示页面
          </el-button>
          <div class="user-info">
            <el-icon class="user-icon">
              <User/>
            </el-icon>
            <span>管理员</span>
            <el-button 
              type="text" 
              @click="logout" 
              class="logout-button"
              size="small">
              退出登录
            </el-button>
          </div>
        </div>
      </el-header>

      <el-container>
        <!-- 美化后的侧边栏 -->
        <el-aside width="220px" class="aside">
          <el-menu
              active-text-color="#409EFF"
              background-color="#f8f9fa"
              class="el-menu-vertical"
              :default-active="'3'"
              text-color="#2c3e50"
          >
            <el-menu-item
                index="3"
                @click="handleClick('/basic-info-data')"
                class="menu-item"
            >
              <el-icon>
                <icon-menu/>
              </el-icon>
              <span>基本信息管理</span>
            </el-menu-item>

            <el-menu-item
                index="4"
                @click="handleClick('/photoData')"
                class="menu-item"
            >
              <el-icon>
                <setting/>
              </el-icon>
              <span>图片数据管理</span>
            </el-menu-item>

            <el-menu-item
                index="5"
                @click="handleClick('/memberData')"
                class="menu-item"
            >
              <el-icon>
                <setting/>
              </el-icon>
              <span>成员信息管理</span>
            </el-menu-item>
          </el-menu>
        </el-aside>

        <el-main>
          <div class="DataContent">
            <RouterView/>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style scoped>
/* 头部样式 */
.header {
  height: 60px;
  background: linear-gradient(135deg, #409EFF 0%, #337ecc 100%);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 30px;
}

.title {
  color: white;
  font-size: 20px;
  font-weight: 600;
}

.back-button {
  margin-left: auto;
  margin-right: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  color: white;
  gap: 8px;
}

.user-icon {
  font-size: 18px;
}

.logout-button {
  margin-left: 12px;
  color: white !important;
  text-decoration: underline;
}

.logout-button:hover {
  color: #f0f0f0 !important;
}

/* 侧边栏样式 */
.aside {
  background-color: #f8f9fa;
  border-right: 1px solid #e9ecef;
  height: calc(100vh - 60px);
}

.el-menu-vertical {
  border-right: none;
  padding: 16px 0;
}

.menu-item {
  height: 50px;
  margin: 4px 12px;
  border-radius: 8px;
  transition: all 0.3s;
}

.menu-item:hover {
  background-color: #e9f3ff !important;
}

.menu-item.is-active {
  background-color: #e9f3ff !important;
}

.el-menu-item span {
  margin-left: 12px;
  font-size: 14px;
}

/* 主内容区 */
.el-main {
  background-color: #f8f9fa;
  padding: 24px;
}

.DataContent {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  min-height: calc(100vh - 108px);
  padding: 24px;
}
</style>